package br.brpv.silvestrin.conferencia.utils;

import java.util.Calendar;

import br.brpv.silvestrin.conferencia.R;



import android.app.DatePickerDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

public class TextViewDatePicker extends TextView {
    private int mYear;
    private int mMonth;
    private int mDay;

	public TextViewDatePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		initDate();
		updateText();
		
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DatePickerDialog picker = new DatePickerDialog(getContext(), mDateSetListener, mYear, mMonth, mDay);
				picker.setTitle(R.string.date_picker_title);
				//picker.setButton(DatePickerDialog.BUTTON_POSITIVE, "Selecionar", new Message());
				//picker.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Cancelar", new Message());
				picker.show();
			}
		});
	}
	
	public String getDate() {
		return mDay + "/" + (mMonth + 1) + "/" + (mYear - 2000);
	}
	
	private void initDate() {
		Calendar c = Calendar.getInstance();
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mMonth = c.get(Calendar.MONTH);
		mYear = c.get(Calendar.YEAR);
	}
	
	private void updateText() {
		setText(mDay + " / " + ((mMonth + 1) < 10? "0" + (mMonth + 1) : (mMonth + 1)) + " / " + (mYear - 2000));
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
		    new DatePickerDialog.OnDateSetListener() {
		        public void onDateSet(DatePicker view, int year, 
		                              int monthOfYear, int dayOfMonth) {
		            mYear = year;
		            mMonth = monthOfYear;
		            mDay = dayOfMonth;
		            updateText();
		        }
		    };

}
